import { director, macro } from 'cc';

//创建一个定时器
class ScheduleUpdater {
    private _running = false;
    private _isPause = false;

    call = (self: ScheduleUpdater) => {};

    start(call: (self: ScheduleUpdater) => void) {
        if (this._running) return;
        this._running = true;
        this._isPause = false;
        director
            .getScheduler()
            .schedule(
                this.update,
                this,
                0,
                macro.REPEAT_FOREVER,
                0,
                false,
            );
    }

    stop() {
        if (!this._running) return;
        this._running = false;

        director.getScheduler().unschedule(this.update, this);
    }

    update(dt?: number) {
        if (this._isPause) {
            return;
        }
        console.log('Global update:', dt);

        if (!this._running) {
            this.stop();
        }
        this.call(this);
    }

    //暂停
    pause() {}

    // 继续
    resume() {}
}

//Example
// const updater = new MyUpdater();
// updater.start((self) => {});
